﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class city_s_upload_pic : System.Web.UI.Page
{

    int no;
    ct_s_profile cts;
    ctpDataContext db;

    protected void Page_Load(object sender, EventArgs e)
    {

        no = int.Parse(Request["no"]);
        ctp_lib.check_login(this.Page, "city_s_upload_pic.aspx?no=" + no.ToString());

        db = new ctpDataContext();

         cts = db.ct_s_profiles.Single(p => p.valid == true && p.no == no);


        if (!this.IsPostBack)
        {

            

            this.HyperLink3.Text = cts.title;
            this.HyperLink3.NavigateUrl = "city_s.aspx?no=" + no.ToString().Trim();
            this.HyperLink2.Text = cts.ct_profile.title.Trim();
            this.HyperLink2.NavigateUrl = "city.aspx?no=" + cts.ct_no;

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        
        for (int i = 1; i <= 5; i++)
        {
            FileUpload fu = (FileUpload)this.Panel1.FindControl("FileUpload" + i.ToString());

            if (fu.HasFile)
            {
                
                System.IO.FileInfo fi = new System.IO.FileInfo(fu.PostedFile.FileName);
                string ext = fi.Extension.ToLower();
                if (ext == ".jpeg") ext = ".jpg";
                if (ext != ".gif" && ext != ".jpg") return;

                string fn=Session["userno"].ToString()+"-"+no.ToString()+"-"+db.ct_s_pics.Count(p=>p.ct_s_no==no).ToString()+ext;

                ct_s_pic p1 = new ct_s_pic();
                p1.create_user_no = (int)Session["userno"];
                p1.ct_s_no = no;
                p1.language_no = (int)Session["now_user_lang_no"];
                p1.filename = fn;
                p1.ct_no = cts.ct_no;
                p1.addtime = DateTime.Now;

                db.ct_s_pics.InsertOnSubmit(p1);


                string path = Page.MapPath("city_imgs/");

                
                ctp_lib.savepic(this.Page, fn, fu.PostedFile,path);

                var ctsdata = from p in db.ct_s_profiles
                          where p.no == no && p.main_pic_fn=="coming.gif"
                          select p;

                foreach(ct_s_profile c in ctsdata)
                {
                        c.main_pic_fn = fn;                    
                }

                
                var ctdata = from p in db.ct_profiles
                         where p.no == cts.ct_no && p.main_pic_fn=="coming.gif"
                         select p;

                foreach (ct_profile c in ctdata)
                {
                    c.main_pic_fn =fn;
                }

                db.SubmitChanges();



            }
        }

        Response.Redirect("city_s.aspx?no="+no.ToString());
    }
}
